<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/15
 * Time: 15:43
 */

namespace app\admin\controller;


use app\admin\model\ClassType;
use app\admin\model\DriverType;
use app\admin\model\MakeUpExam;
use think\Controller;

class Makeup extends Controller
{
    //==================================================补考类型
    public function examType(){
        return $this->fetch();
    }

    //==================================================补考数据
    public function examTypeData($page,$limit){
        $data=MakeUpExam::withAttr(['exam_type'=>function($value,$data){
            $driverInfo=DriverType::where('id',$data['exam_type'])->find();
            $data['exam_type']=$driverInfo['name'];
            return $data['exam_type'];
        },'exam_subject'=>function($value,$data){
            $classInfo=ClassType::where('id',$data['exam_subject'])->find();
            $data['exam_subject']=$classInfo['class_name'];
            return $data['exam_subject'];
        }])->page($page,$limit)->order('sort desc')->select();
        $count=MakeUpExam::count();
        $res=['code'=>0,'count'=>$count,'data'=>$data];
        return json($res);
    }

    //==================================================添加补考类型
    public function examTypeAdd(){
        if(request()->isPost()){
            $post=input('post.');
            $examTypeInfo=MakeUpExam::where(['exam_type'=>$post['exam_type'],'exam_subject'=>$post['exam_subject']])->find();
            if($examTypeInfo){
                return json(['code'=>0,'msg'=>'该补考类型已存在']);
            }
            $data['exam_type']=$post['exam_type'];
            $data['exam_subject']=$post['exam_subject'];
            $data['price']=$post['price'];
            $data['sort']=$post['sort'];
            $addres=MakeUpExam::insert($data);
            if($addres){
                $res=['code'=>1,'msg'=>'保存成功'];
            }else{
                $res=['code'=>0,'msg'=>'保存失败'];
            }
            return json($res);
        }
        //查询驾照类型
        $driverTypeData=DriverType::order('sort desc')->select();
        $this->assign('driverTypeData',$driverTypeData);

        //查询可不类型
        $classTypeData=ClassType::select();
        $this->assign('classTypeData',$classTypeData);
        return $this->fetch();
    }

    //==================================================修改补考类型
    public function examTypeEdit(){
        if(request()->isPost()){
            $post=input('post.');
            if($post['exam_type']!=$post['exam_type1']||$post['exam_type']!=$post['exam_type1']){
                $examTypeInfo=MakeUpExam::where(['exam_type'=>$post['exam_type'],'exam_subject'=>$post['exam_subject']])->find();
                if($examTypeInfo){
                    return json(['code'=>0,'msg'=>'该补考类型已存在']);
                }
            }
            $data['exam_type']=$post['exam_type'];
            $data['exam_subject']=$post['exam_subject'];
            $data['price']=$post['price'];
            $data['sort']=$post['sort'];
            $addres=MakeUpExam::where('id',$post['id'])->update($data);
            if($addres){
                $res=['code'=>1,'msg'=>'保存成功'];
            }else{
                $res=['code'=>0,'msg'=>'保存失败'];
            }
            return json($res);
        }
        //查询驾照类型
        $driverTypeData=DriverType::order('sort desc')->select();
        $this->assign('driverTypeData',$driverTypeData);

        //查询可不类型
        $classTypeData=ClassType::select();
        $this->assign('classTypeData',$classTypeData);

        //查询需要修改的补考类型
        $examType=MakeUpExam::where('id',input('get.id'))->find();
        $this->assign('examType',$examType);

        //查询驾照类型
        $driverTypeInfo=DriverType::where('id',$examType['exam_type'])->find();
        $this->assign('driverTypeInfo',$driverTypeInfo);

        //查询可不类型
        $classTypeInfo=ClassType::where('id',$examType['exam_subject'])->find();
        $this->assign('classTypeInfo',$classTypeInfo);
        return $this->fetch();
    }

    //===============================================删除补考类型
    public function examTypeDel(){
        $delres=MakeUpExam::where('id',input('post.id'))->delete();
        if($delres){
           $res=['code'=>0,'msg'=>'保存成功'];
        }else{
            $res=['code'=>1,'msg'=>'保存失败'];
        }
        return json($res);
    }



}